package com.csdj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.reggie.commcn.R;
import com.csdj.reggie.dto.SetmealDto;
import com.csdj.reggie.entity.Category;
import com.csdj.reggie.entity.Dish;
import com.csdj.reggie.entity.Setmeal;
import com.csdj.reggie.entity.SetmealDish;
import com.csdj.reggie.service.CategoryService;
import com.csdj.reggie.service.DishService;
import com.csdj.reggie.service.SetmealDishService;
import com.csdj.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("新增套餐数据信息：{}",setmealDto);
        setmealService.savaWithDish(setmealDto);
        return R.success("新增套餐成功");
    }


    /**
     * 查询套餐数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> list(Long page,Long pageSize,String name){
        // 日志输出
        log.info("分页为:{},{};查询的name：{}",page,pageSize,name);
        // 构造分页器
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        // 构造dto分页器
        Page<SetmealDto> setmealDtoPage=new Page<>();
        // 构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        // 添加模糊查询条件
        queryWrapper.like(name!=null,Setmeal::getName,name);
        // 添加安装修改时间倒序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        // 开始查询数据
        setmealService.page(pageInfo,queryWrapper);
        // 备份pageInfo的数据到dto构造器上
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list=records.stream().map((item)->{
            SetmealDto dto=new SetmealDto();
            // 对象拷贝
            BeanUtils.copyProperties(item,dto);
            // 获取分类id
            Long caId = item.getCategoryId();
            // 根据id获取分类数据
            Category category = categoryService.getById(caId);
            // 判断是否为空
            if (category!=null){
                // 获取分类name
                String cname = category.getName();
                //赋值
                dto.setCategoryName(cname);
            }
            return dto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        // 返回数据
        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除id为：{}的套餐",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除菜品成功");
    }

    @PostMapping("/status/{status}")
    public R<String> update(@PathVariable Integer status,Long[] ids){
        log.info("修改id为：{}，套餐状态为：{}",ids,status);
        for (int i=0;i<ids.length;i++){
            Setmeal setmeal = setmealService.getById(ids[i]);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("状态修改成功");
    }

    /**
     * 根据条件查询套餐数据
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
